//
//  YTModifyInfoViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/7.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTModifyInfoViewModel.h"
#import "YTUserProfileServices.h"

@interface YTModifyInfoViewModel ()

@property (strong, nonatomic, readwrite) RACCommand *changeAvatarCommand;
@property (strong, readwrite, nonatomic) RACCommand *liveCommand;
@property (strong, readwrite, nonatomic) RACCommand *serviceCommand;
@property (strong, readwrite, nonatomic) RACCommand *languageCommand;
@property (strong, readwrite, nonatomic) RACCommand *saveInfoCommand;

@end

@implementation YTModifyInfoViewModel

- (void)initialize
{
    [self loadSelectedArray];
    [self modifyAvatar];
    [self citySelect];
    [self saveInfo];
}

/** 修改头像*/
- (void)modifyAvatar
{
    @weakify(self);
    _changeAvatarCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            dict[@"avatarImage"] = [UIImage yt_imageToString:self.avatarImage];
            [[YTUserProfileServices changeAvatarImage:dict] subscribeNext:^(id x) {
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
}

/** 选择居住城市和服务城市*/
- (void)citySelect
{
    YTProfileModel *model = [YTUtilityManager manager].userModel;
    self.serviceCity = [NSString string];
    self.liveCity = [NSString string];
    self.language = [NSString string];
    for (YTProfileServiceCityModel *cityModel in model.serviceCity) {
        self.serviceCity = [self.serviceCity stringByAppendingString:[NSString stringWithFormat:@" %@", cityModel.Name]];
    }
    self.liveCity = model.cityName;
    for (YTProfileServiceLanguageModel *languageModel in model.language) {
        self.language = [self.language stringByAppendingString:[NSString stringWithFormat:@" %@", languageModel.name]];
    }
    
    @weakify(self);
    _liveCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(RACTuple *input) {
        @strongify(self);
        NSArray<NSString *> *cityArray = input.first;
        NSArray<NSString *> *cityIDArray = input.second;
        self.liveCityID = cityIDArray[0];
        self.liveCity = cityArray[0];
        return [RACSignal empty];
    }];
    
    _serviceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(RACTuple *input) {
        NSArray<NSString *> *cityArray = input.first;
        NSArray<NSString *> *cityIDArray = input.second;
        NSMutableString *text = [[NSMutableString alloc] init];
        NSMutableString *idText = [[NSMutableString alloc] init];
        
        for (NSInteger i = 0; i < cityArray.count; i++) {
            [text appendFormat:@" %@", cityArray[i]];
            if (i == 0) {
                [idText appendString:cityIDArray[i]];
            } else {
                [idText appendFormat:@",%@", cityIDArray[i]];
            }
        }
        self.serviceCity = text;
        self.serviceCityID = idText;
        
        return [RACSignal empty];
    }];
}

/** 加载职业,居住年限,学历数据*/
- (void)loadSelectedArray
{
    _lifeYearArray = [NSMutableArray array];
    _jobArray = [NSMutableArray array];
    _educationArray = [NSMutableArray array];
    [self loadSeletedArrayWith:_jobArray type:@"job"];
    [self loadSeletedArrayWith:_educationArray type:@"education"];
    [self loadSeletedArrayWith:_lifeYearArray type:@"lifeyear"];
}

- (void)loadSeletedArrayWith:(NSMutableArray<NSString *> *)arrray
                        type:(NSString *)type {
    [[YTUserProfileServices loadUserJobEducationLifeYear:type]
     subscribeNext:^(NSDictionary *response) {
         NSArray *dataArray = response[@"responseData"];
         for (NSDictionary *dict in dataArray) {
             [arrray addObject:dict[@"name"]];
         }
     }];
}

/** 保存修改信息*/
- (void)saveInfo
{
    _saveInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *info) {
        return [YTUserProfileServices saveChangeUserInfo:info];
    }];
}
@end


